package com.android.sgzcommon.activity;

import android.content.Intent;
import android.os.Bundle;

import com.android.sgzcommon.take_photo.GetPhoto;
import com.android.sgzcommon.take_photo.GetPhotoList;
import com.android.sgzcommon.take_photo.GetPhotoListImpl;
import com.android.sgzcommon.take_photo.ShowImages;
import com.android.sgzcommon.take_photo.ShowImagesImpl;
import com.android.sgzcommon.take_photo.entity.PhotoUpload;
import com.android.sgzcommon.take_photo.listener.OnPhotoClickListener;
import com.android.sgzcommon.take_photo.listener.OnPhotoUploadListener;
import com.android.sgzcommon.take_photo.listener.OnTakePhotoListListener;
import com.android.sgzcommon.take_photo.utils.Creator;

import java.util.List;
import java.util.Map;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by sgz on 2019/5/10 0010.
 */
public abstract class BaseGetPhotoListActivity<T extends PhotoUpload> extends BaseActivity implements GetPhotoList<T>, GetPhoto {

    private RecyclerView mShowImageRecyclerView;
    private RecyclerView mGetPhotoRecyclerView;
    private GetPhotoListImpl mGetPhotoListImpl;
    private ShowImages mShowImageGrid;

    @LayoutRes
    protected abstract int getContentViewId();

    @IdRes
    protected abstract int getShowImageGridViewId();

    @IdRes
    protected abstract int getTakePhotoGridViewId();

    protected abstract Creator<T> getCreator();

    /**
     *
     * @param ts
     * @param t 刚刚拍摄或选取的照片
     */
    protected abstract void onPhotos(List<T> ts, T t);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());
        mGetPhotoRecyclerView = findViewById(getTakePhotoGridViewId());
        mShowImageRecyclerView = findViewById(getShowImageGridViewId());
        mShowImageGrid = new ShowImagesImpl(this, mShowImageRecyclerView);
        Creator creator = getCreator();
        if (creator == null){
            creator = new Creator<PhotoUpload>() {
                @Override
                public PhotoUpload create(String path) {
                    return new PhotoUpload(path);
                }
            };
        }
        mGetPhotoListImpl = new GetPhotoListImpl(this, creator,mGetPhotoRecyclerView);
        mGetPhotoListImpl.setOnPhotoListListener(new OnTakePhotoListListener<T>() {
            @Override
            public void onPhotos(List<T> ts, T t) {
                BaseGetPhotoListActivity.this.onPhotos(ts, t);
            }
        });
    }

    public void setImageUrls(List<String> urls) {
        mShowImageGrid.setImageUrls(urls);
    }

    public void takePhoto() {
        mGetPhotoListImpl.takePhoto();
    }

    @Override
    public void choosePhoto() {
        mGetPhotoListImpl.choosePhoto();
    }

    @Override
    public void setOnPhotoClickListener(OnPhotoClickListener listener) {
        mGetPhotoListImpl.setOnPhotoClickListener(listener);
    }

    @Override
    public void setOnPhotoListListener(OnTakePhotoListListener listener) {
        mGetPhotoListImpl.setOnPhotoListListener(listener);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mGetPhotoListImpl.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mGetPhotoListImpl.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public void notifyShowPhotoChanged() {
        mShowImageGrid.notifyPhotoChanged();
    }

    @Override
    public void notifyPhotoChanged() {
        mGetPhotoListImpl.notifyPhotoChanged();
    }

    @Override
    public void notifyPhotoChanged(int position) {
        mGetPhotoListImpl.notifyPhotoChanged(position);
    }

    @Override
    public List<T> getPhotoUploadList() {
        return mGetPhotoListImpl.getPhotoUploadList();
    }

    @Override
    public void addPhotoUpload(T t) {
        mGetPhotoListImpl.addPhotoUpload(t);
    }

    @Override
    public void addPhotoUploads(List<T> ts) {
        mGetPhotoListImpl.addPhotoUploads(ts);
    }

    @Override
    public void clearPhotos() {
        mGetPhotoListImpl.clearPhotos();
    }

    @Override
    public void uploadPhotos(String url, Map<String, String> data, Map<String, String> headers, OnPhotoUploadListener listener) {
        mGetPhotoListImpl.uploadPhotos(url, data, headers, listener);
    }

}
